import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { RiskControlRuleNewBaseClass } from '../baseClass'
import { RiskRuleStoreService } from '../rule-store.service'
import { FormBuilder, FormGroup, Validators } from '@angular/forms'
import { NzMessageService } from 'ng-zorro-antd'
import { IFormItem, FormItemType, IOption } from '@shared/interfaces/common'
import { RiskRuleFormService } from './risk-rule-form.service'
import { RiskRuleModeEnum, RiskControlRule } from '@shared/interfaces/risk-control'
import { RiskService } from 'app/routes/risk/risk.service'
import { Router, ActivatedRoute } from '@angular/router'
import { makeAllControlDirty } from 'app/utils/helpers'
import { I18NService } from '@core'

@Component({
  selector: 'app-risk-rule-new-base',
  templateUrl: './base.component.html',
})
export class RiskRuleNewBaseComponent extends RiskControlRuleNewBaseClass implements OnInit {

  @ViewChild('currency') currency: TemplateRef<void>
  schemeCode: string // 风控方案 code，从 url 中获取

  formItemType = FormItemType
  formItems: IFormItem[] = []

  modeOptions: IOption[] = [
    { label: this.trans('RISK_CTRL_RULE_MODE.SINGLE'), value: '00' },
    { label: this.trans('RISK_CTRL_RULE_MODE.SINGLE_DAY'), value: '01' },
    { label: this.trans('RISK_CTRL_RULE_MODE.SINGLE_MONTH'), value: '30' },
    { label: this.trans('RISK_CTRL_RULE_MODE.REFUND'), value: 'refund' },
  ]

  modeFormItem: IFormItem = {
    label: this.trans('RISK_CTRL.RULE.MODE'),
    errTip: '请选择规则模式',
    formControlName: 'mode',
    type: FormItemType.SELECT,
    multiple: 'default',
    allowClear: false,
    options: this.modeOptions,
    compareFn: (o1, o2) => o1 === o2,
    change: this.switchMode.bind(this)
  }

  constructor(
    public fb: FormBuilder,
    public riskRuleStore: RiskRuleStoreService,
    public router: Router,
    public route: ActivatedRoute,
    private msg: NzMessageService,
    private riskRuleFormService: RiskRuleFormService,
    private riskService: RiskService,
    private i18n: I18NService,
  ) {
    super(fb, riskRuleStore)
  }

  ngOnInit() {
    this.riskRuleFormService.initTemplates({ currency: this.currency })
    this.formItems = this.initFormItems()
    this.validateForm = this.initValidateForm()
  }

  initFormItems(): IFormItem[] {
    return [this.modeFormItem, ...this.riskRuleFormService.getFormItems(RiskRuleModeEnum.Trade)]
  }

  initValidateForm(): FormGroup {
    return this.riskRuleFormService.getValidateForm(RiskRuleModeEnum.Trade)
  }

  switchMode(mode: RiskRuleModeEnum): void {
    this.formItems = [this.modeFormItem, ...this.riskRuleFormService.getFormItems(mode)]
    this.validateForm = this.riskRuleFormService.getValidateForm(mode)
  }

  checkValid(): boolean {
    const valid = this.validateForm.valid
    if (valid) {
      this.pushDataToStore()
      this.msg.success(this.trans('GENERAL.NOTIFICATION.VALIDATION_SUCCESS'))
    } else {
      makeAllControlDirty(this.validateForm)
      this.msg.error(this.trans('GENERAL.NOTIFICATION.VALIDATION_FAIL'))
    }
    return valid
  }

  pushDataToStore(): void {
    const { currency, ...rest } = this.validateForm.value
    const data: RiskControlRule = {
      currency: currency.code,
      ...rest,
    }
    if (this.validateForm.value.quota) {
      /** quota 类型是 number */
      data.quota = parseInt(this.validateForm.value.quota, 10)
    }

    if (this.validateForm.value.quotaStatic) {
      data.quotaStatic = parseInt(this.validateForm.value.quotaStatic, 10)
      data.scene = 'dynamic'
    }

    this.riskRuleStore.setValue(data)
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
